// 老师获取指定作业的完成情况
import express from "express";
import pool from "../../importantApi/db_connection.js";

let router = express.Router()
router.get("/", async (req, res) => {
    let {homeworkId} = req.query;
    if (!homeworkId) {
        return res.status(201).json({
            code: 201,
            error: "缺少必要参数"
        })
    }
    let submittedSql = `
        SELECT u.user_id as student_id,
               u.user_name as student_name,
               d.dowork_id,
               d.createtime as dowork_time,
               IF(r.dowork_id IS NOT NULL, 1, 0) AS is_reviewed,
               r.score,
               r.comment
        FROM homework h
                 INNER JOIN user_class uc ON h.class_id = uc.class_id
                 INNER JOIN userinfo u ON uc.user_id = u.user_id AND u.identity = 'student'
                 INNER JOIN dowork d ON d.homework_id = h.homework_id AND d.student_id = u.user_id
                 LEFT JOIN reviewwork r ON r.dowork_id = d.dowork_id
        WHERE h.homework_id = ${homeworkId};
    `
    let unsubmittedSql = `
        SELECT u.user_id as student_id,
               u.user_name as student_name
        FROM homework h
                 INNER JOIN user_class uc ON h.class_id = uc.class_id
                 INNER JOIN userinfo u ON uc.user_id = u.user_id AND u.identity = 'student'
                 LEFT JOIN dowork d ON d.homework_id = h.homework_id AND d.student_id = u.user_id
        WHERE h.homework_id = ${homeworkId}
          AND d.dowork_id IS NULL;
    `
    try {
        const [submitted, unsubmitted] = await Promise.all([
            pool.query(submittedSql),
            pool.query(unsubmittedSql)
        ])
        return res.status(200).json({
            code: 200,
            data: {
                submitted: submitted[0], unsubmitted: unsubmitted[0]
            }
        })
    } catch (err) {
        console.log(err)
        return res.status(500).json({
            code: 500,
            error: err,
            msg: "查询错误：server error"
        })
    }
})
export default router;